package com.zjb.datastructure.link;

public class Stack<T> {

    private Object[] arr;

    private int curIndex;


    public Stack(int size) {
        arr = new Object[size];
        curIndex = 0;
    }

    void push(T t) {
        checkSize();
        arr[curIndex] = t;
        curIndex++;
    }

    private void checkSize() {
        if (curIndex > arr.length - 1) {
            Object[] newArr = new Object[arr.length * 2];
            System.arraycopy(arr, 0, newArr, 0, arr.length);
            arr = newArr;
        }
    }

    T pop() {
        T result = (T) arr[curIndex-1];
        curIndex--;
        return result;
    }

    public static void main(String[] args) {

        Stack<Integer> stack = new Stack<>(2);
        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        stack.push(3);
    }

}
